package resourse;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

import sun.awt.shell.ShellFolder;




/**
 * 
 * @author ����  QQ:249782944
 * ѧУ:���ϴ�ѧ ��ѧԺ
 * EnterpriseIMServer :��ҵ��ʱͨѶ
 * Apr 27, 2010
 * 
 * ������Դ������
 */
public class ResourseMannager {

	/**
	 * ͨ��ͼ������ֻ��ͼ�����
	 * 
	 * �����ͼ��������ResourseMannager��ͬ��Ŀ¼��
	 * 
	 * @param iconName:����ͼ�������
	 * @return�����ش���ͼ�����
	 */
	public static ImageIcon getIconUrlByName(String iconName) {
		java.net.URL iconUrl = ResourseMannager.class.getResource(iconName);
		javax.swing.ImageIcon icon = new javax.swing.ImageIcon(iconUrl);
		return icon;
	}

	/**
	 * 
	 * @param imagename
	 * @return
	 */
	public static Icon getIcon(String imagename) {
		java.net.URL url = ResourseMannager.class.getResource(imagename);
		Icon icon = new ImageIcon(url);
		return icon;
	}
	/**
	 * 
	 * @param ext
	 * @return
	 */
	public static Icon getIconByExtractor(String ext,boolean isBig) {
		File file=null;
		Icon icon=null;
		try {
			file = File.createTempFile("icon", "." + ext);
		    FileSystemView view = FileSystemView.getFileSystemView();
			if(isBig){
		    	  ShellFolder shellFolder = ShellFolder.getShellFolder(file);
		    	  icon = new ImageIcon(shellFolder.getIcon(true));
		      }else{
		    	  icon= view.getSystemIcon(file);
		      }
			file.delete();
		} catch (IOException e) {
			e.printStackTrace();
			icon=getIcon("computer.png");
		}
		
		return icon;
	}
	public static Image getImage(String imagename) {
		java.net.URL url = ResourseMannager.class.getResource(imagename);
		Image icon = Toolkit.getDefaultToolkit().createImage(url);
		return icon;
	}
	/**
	 * 
	 * @param imagePath �����ResourcesManagement��·��
	 * @return
	 */
	public static Image getImage(String imagePath,int x,int y){
	    java.net.URL url=ResourseMannager.class.getResource(imagePath);
		Image image = Toolkit.getDefaultToolkit().getImage(url);
		image = image.getScaledInstance(x,y, Image.SCALE_SMOOTH); 
		return image;
	}
	
	/**
	 * 
	 * @param imagePath
	 * @return
	 */
	public static ImageIcon getImageIcon(String imagePath,int x,int y){
		ImageIcon imageIcon=getIconUrlByName(imagePath);
		int width=imageIcon.getIconWidth();
		int height=imageIcon.getIconHeight();
		float xbili=(float) ((x*1.0)/width);
		float ybili=(float) ((y*1.0)/height);
		float bili=xbili>ybili?ybili:xbili;
		int newwidth=(int) (width*bili);
		int newheight=(int) (height*bili);
		Image image=getImage(imagePath,newwidth,newheight);
		imageIcon.setImage(image);
		return imageIcon;
	}
	
	public static String[] getWallImageNames(){
		String[] strs=null;
		String path=ResourseMannager.class.getPackage().getName().replace(".", "/");
		File file=new File("src/"+path+"/wallimage");
		FileListFilter filter=new FileListFilter();
		if(file.isDirectory()){
			File[] files=file.listFiles(filter);
			strs=new  String[files.length];
			for(int i=0;i<files.length;i++){
				strs[i]=files[i].getName();
			}
			return strs;
		}
		strs=new String[0];
		return strs;
	}
}
class FileListFilter implements FilenameFilter {
	private String extension="jpg";
	public boolean accept(File dir, String name) {
		boolean fileOK = true;
		if (extension != null) {
		   fileOK = name.endsWith('.' + extension);
		}
		return fileOK;
	}
}
